package com.itheima.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.CheckItemMapper;
import com.itheima.mapper.CheckgroupCheckitemMapper;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import com.itheima.utils.HealthStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @description: 检查项的业务层
 * @author: wangjingliang
 * @create: 2019-11-06 19:34
 **/
@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {
    @Autowired
    private CheckItemMapper checkItemMapper;
    @Autowired
    private CheckgroupCheckitemMapper checkgroupCheckitemMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 添加
     *
     * @param: CheckItem
     * @return: null
     * @date: 2019/11/6
     */
    @Override
    public int addAll(CheckItem checkItem) {

        //判断编码和名称是否已经存在
        int byCode = checkItemMapper.findByCode(checkItem.getCode());
        if (byCode > 1) {
            throw new RuntimeException("该编码已存在");
        }
        int byName = checkItemMapper.findByName(checkItem.getName());
        if (byName > 1) {
            throw new RuntimeException("请该名称已存在");
        }

        //都无误变进行数据的添加
        int addAll = checkItemMapper.insert(checkItem);
        if (checkItem.getId() != null) {
            //存入redis中
            redisTemplate.boundListOps("checkItemList").rightPush(checkItem);
        }

        return addAll;
    }

    /**
     * 根据页码和当前显示页数进行分页查询
     *
     * @param: QueryPageBean
     * @return: null
     * @date: 2019/11/7
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {


        Integer pageSize = queryPageBean.getPageSize();
        Integer currentPage = queryPageBean.getCurrentPage();
        String queryString = queryPageBean.getQueryString();
        //判断参数是否存在
        if (currentPage < 1 || currentPage == null) {
            currentPage = 1;
        }
        if (pageSize < 0 || pageSize == null) {
            pageSize = 10;
        }
        if (queryString == null) {
            queryString = "";
        }
        //判断是否为null或者为" "
        if (HealthStringUtils.isEmpty(queryString)) {

            queryString = queryString.trim();
        }
        //基于mybatis的分页插件
        PageHelper.startPage(currentPage, pageSize);
        Page<CheckItem> page = checkItemMapper.findPage("%" + queryString + "%");

        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 根据id进行查询
     *
     * @param:Integer id
     * @return: CheckItem
     * @date: 2019/11/7
     */
    @Override
    public CheckItem findById(Integer id) {
        //CheckItem byId = checkItemMapper.findById(id);
        CheckItem Item = null;
        //直接从redis中进行查询
        List<CheckItem> checkItemList = redisTemplate.boundListOps("checkItemList").range(0, -1);
        for (CheckItem checkItem : checkItemList) {
            if (checkItem.getId().equals(id)) {
                Item = checkItem;
            }
        }
        return Item;
    }

    /**
     * 根据提供数据进行修改
     *
     * @param: CheckItem
     * @return: int
     * @date: 2019/11/7
     */
    @Override
    public int edit(CheckItem checkItem) {
        int edit = 0;

        //判断编码和名称是否为null或者是空
        String code = checkItem.getCode();
        String name = checkItem.getName();
        //首先去判读是否和原来的一样,方法是根据id查询

        CheckItem checkItem1 = checkItemMapper.findById(checkItem.getId());


        if (!checkItem1.getName().equals(name)) {
            //判读是否能查询到其他的值

            int byName = checkItemMapper.findByName(name);
            if (byName > 0) {
                throw new RuntimeException("请该名称已存在");
            }
        }
        if (!checkItem1.getCode().equals(checkItem.getCode())) {
            //判断编码和名称是否已经存在
            int byCode = checkItemMapper.findByCode(code);
            if (byCode > 0) {
                throw new RuntimeException("该编码已存在");
            }
        }

        //进行修改
        edit = checkItemMapper.edit(checkItem);

        if (edit > 0) {

            //将集合储存到redis缓存中
            List<CheckItem> checkItemList = redisTemplate.boundListOps("checkItemList").range(0, -1);
            for (CheckItem checkItem2 : checkItemList) {
                if (checkItem.getId().equals(checkItem2.getId())) {
                    redisTemplate.opsForList().remove("checkItemList", 1, checkItem2);
                    redisTemplate.boundListOps("checkItemList").leftPush(checkItem);
                }
            }

        }


        return edit;
    }

    /**
     * 根据id进行删除
     *
     * @param:id
     * @return: int
     * @date: 2019/11/7
     */
    @Override
    public int deleteById(Integer id) {

        //在对数据删除前必须先对中间表的数据进行删除
        //那就先对中间表中数据进行判断
        List<Integer> checkGroupIds = checkgroupCheckitemMapper.findCheckGroupIdByCheckItemIds(id);
        if (checkGroupIds != null && checkGroupIds.size() > 0) {
            int deleteByCheckitemId = checkgroupCheckitemMapper.deleteByCheckitemId(id);
            if (deleteByCheckitemId == 0) {
                throw new RuntimeException(MessageConstant.EXCEPION);
            }
        }
        int deleteById = checkItemMapper.deleteById(id);
        if (deleteById == 0) {
            return deleteById;
        }
        //删除数据库的同时对redis中的数据进行删除
        List<CheckItem> checkItemList = redisTemplate.boundListOps("checkItemList").range(0, -1);
        for (CheckItem checkItem : checkItemList) {
            if (checkItem.getId().equals(id)) {
                redisTemplate.boundListOps("checkItemList").leftPop();
            }
        }
        return deleteById;
    }

    /**
     * 查询所有
     *
     * @param:null
     * @return:List<CheckItem>
     * @date: 2019/11/8
     */
    @Override
    public List<CheckItem> findAll() {


        //从redis中调数据
        List<CheckItem> checkItemList = redisTemplate.boundListOps("checkItemList").range(0, -1);
        return checkItemList;
    }
}
